
package com.uit.ecommerce.access.mapper;

import com.uit.ecommerce.dto.WeatherCountryDTO;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;

public class WeatherCountryMapper extends MapperDB {
    public WeatherCountryMapper() throws Exception{
        super();
    }
    private void InitWeatherCountryDTOFromRs(WeatherCountryDTO weatherCountry, ResultSet rs) throws Exception{
        if((rs != null) && (weatherCountry != null)){
           weatherCountry.setCountryId(rs.getString("MaNuoc"));
           weatherCountry.setCountryName(rs.getString("TenNuoc"));
           weatherCountry.setDate(rs.getString("Ngay"));
           weatherCountry.setTempMin(Float.parseFloat(rs.getString("NhietDoThapNhat")));
           weatherCountry.setTempMax(Float.parseFloat(rs.getString("NhietDoCaoNhat")));
           weatherCountry.setNote(rs.getString("ThongTin"));
           weatherCountry.setWeather(rs.getString("DuBao"));
        }
    }
     public ArrayList<WeatherCountryDTO> GetAllWeatherCountry() throws Exception{
        ArrayList<WeatherCountryDTO> listResult = new ArrayList<WeatherCountryDTO>();
        try{
            StringBuffer sql = new StringBuffer();
            sql.append("select datnuoc. MaNuoc, TenNuoc, Ngay, NhietDoCaoNhat, NhietDoThapNhat,ThongTin,DuBao from ecormerce.datnuoc, ecormerce.thoitietdatnuoc where datnuoc.MaNuoc=thoitietdatnuoc.MaNuoc");
             PreparedStatement stmt = getConnection().prepareStatement(sql.toString());
            ResultSet rs = stmt.executeQuery();
           while((rs != null) && (rs.next())){
                WeatherCountryDTO temp = new WeatherCountryDTO();
                InitWeatherCountryDTOFromRs(temp, rs);
                listResult.add(temp);
            }
        }catch(Exception ex){
            throw ex;
        }

        return listResult;
    }
     public void insertWeatherCountry(WeatherCountryDTO cls) throws Exception{
    try{
            StringBuffer sql = new StringBuffer();
            sql.append("Insert into ecormerce.thoitietdatnuoc values('"+cls.getCountryId()+"','"+cls.getDate()+"',"+cls.getTempMax()+","+cls.getTempMin()+",'"+cls.getNote()+"','"+cls.getWeather()+"')");
             PreparedStatement stmt = getConnection().prepareStatement(sql.toString());
            stmt.execute();
            stmt.close();
        }catch(Exception ex){
            throw ex;
        }
}
public void DeleteWeatherCountry(WeatherCountryDTO cls) throws Exception{
    try{
            StringBuffer sql = new StringBuffer();
            sql.append("Delete from ecormerce.thoitietdatnuoc where MaNuoc='");
            sql.append(cls.getCountryId()).append("' and Ngay='"+cls.getDate()+"'");
             PreparedStatement stmt = getConnection().prepareStatement(sql.toString());
            stmt.execute();
            stmt.close();
        }catch(Exception ex){
            throw ex;
        }
}
public boolean checkWeatherCountryExit(WeatherCountryDTO cls) throws Exception{
     try{
            StringBuffer sql = new StringBuffer();
            sql.append("select * from ecormerce.thoitietdatnuoc where MaNuoc='");
            sql.append(cls.getCountryId()).append("' and Ngay='"+cls.getDate()+"'");
             PreparedStatement stmt = getConnection().prepareStatement(sql.toString());
            ResultSet rs = stmt.executeQuery();
           if((rs != null) && (rs.next())){
                return true;//đã tồn tại
            }
        }catch(Exception ex){
            throw ex;
        }
     return  false;//chưa tồn tại
}
 public ArrayList<WeatherCountryDTO> GetAllWeatherCountryByName(String Name) throws Exception{
        ArrayList<WeatherCountryDTO> listResult = new ArrayList<WeatherCountryDTO>();
        try{
            StringBuffer sql = new StringBuffer();
            sql.append("select datnuoc. MaNuoc, TenNuoc,Ngay,NhietDoCaoNhat, NhietDoThapNhat,ThongTin,DuBao from ecormerce.datnuoc, ecormerce.thoitietdatnuoc where datnuoc.MaNuoc=thoitietdatnuoc.MaNuoc and TenNuoc='"+Name+"'");
             PreparedStatement stmt = getConnection().prepareStatement(sql.toString());
            ResultSet rs = stmt.executeQuery();
           while((rs != null) && (rs.next())){
                WeatherCountryDTO temp = new WeatherCountryDTO();
                InitWeatherCountryDTOFromRs(temp, rs);
                listResult.add(temp);
            }
        }catch(Exception ex){
            throw ex;
        }

        return listResult;
    }
 public ArrayList<WeatherCountryDTO> GetAllWeatherCountryByCode(String Id) throws Exception{
        ArrayList<WeatherCountryDTO> listResult = new ArrayList<WeatherCountryDTO>();
        try{
            StringBuffer sql = new StringBuffer();
            sql.append("select datnuoc. MaNuoc, TenNuoc, Ngay, NhietDoCaoNhat, NhietDoThapNhat,ThongTin,DuBao from ecormerce.datnuoc, ecormerce.thoitietdatnuoc where datnuoc.MaNuoc=thoitietdatnuoc.MaNuoc and datnuoc.MaNuoc='"+Id+"'");
             PreparedStatement stmt = getConnection().prepareStatement(sql.toString());
            ResultSet rs = stmt.executeQuery();
           while((rs != null) && (rs.next())){
                WeatherCountryDTO temp = new WeatherCountryDTO();
                InitWeatherCountryDTOFromRs(temp, rs);
                listResult.add(temp);
            }
        }catch(Exception ex){
            throw ex;
        }

        return listResult;
    }
  public ArrayList<WeatherCountryDTO> GetAllWeatherCountryByDate(String Date) throws Exception{
        ArrayList<WeatherCountryDTO> listResult = new ArrayList<WeatherCountryDTO>();
        try{
            StringBuffer sql = new StringBuffer();
            sql.append("select datnuoc. MaNuoc, TenNuoc, Ngay, NhietDoCaoNhat, NhietDoThapNhat,ThongTin,DuBao from ecormerce.datnuoc, ecormerce.thoitietdatnuoc where datnuoc.MaNuoc=thoitietdatnuoc.MaNuoc and Ngay='"+Date+"'");
             PreparedStatement stmt = getConnection().prepareStatement(sql.toString());
            ResultSet rs = stmt.executeQuery();
           while((rs != null) && (rs.next())){
                WeatherCountryDTO temp = new WeatherCountryDTO();
                InitWeatherCountryDTOFromRs(temp, rs);
                listResult.add(temp);
            }
        }catch(Exception ex){
            throw ex;
        }

        return listResult;
    }
  public WeatherCountryDTO GetWeatherCountryByDateAndName(String Date, String Name) throws Exception{
       WeatherCountryDTO Result = new WeatherCountryDTO();
        try{
            StringBuffer sql = new StringBuffer();
            sql.append("select datnuoc. MaNuoc, TenNuoc, Ngay, NhietDoCaoNhat, NhietDoThapNhat,ThongTin,DuBao from ecormerce.datnuoc, ecormerce.thoitietdatnuoc where datnuoc.MaNuoc=thoitietdatnuoc.MaNuoc and Ngay='"+Date+"' and TenNuoc='"+Name+"'");
             PreparedStatement stmt = getConnection().prepareStatement(sql.toString());
            ResultSet rs = stmt.executeQuery();
           while((rs != null) && (rs.next())){
                 InitWeatherCountryDTOFromRs(Result, rs);
            }
        }catch(Exception ex){
            throw ex;
        }

        return Result;
    }
}
